package project.active;

import java.util.ArrayList;

import model.pojo.Employee;
import model.pojo.Languages;
import project.model.PrProject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.TableLayout;
import android.widget.TextView;
import approval.dao.LangDAO;
import approve.action.LoginActivity;
import approve.action.R;

public class ProjectManagerDetail extends Activity{
	TableLayout detailProjectManager;
	TextView id,projectName,manager,status,statusDate,nameProject;
	RadioButton bttMember,bttError,bttTester;
	PrProject prProject=null;
	
	TextView txtID, txtPrName,txtManager,txtStatus,txtStatys, txtNamePr;
	private boolean read = true;
	private Bundle alllangs = new Bundle();
	private String lang="EN",database="admin";
	Employee emp=new Employee();
	TextView logout,username;
	
	Bundle language;
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("databaseMember");
		}
		return database;
	}

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		language = getIntent().getExtras().getBundle(getLang());
		setContentView(R.layout.projectmanagerdetail);
		
		initAttribute();
//		detailProjectManager = (TableLayout)findViewById(R.id.tableLayoutProjectManagerDetail);
		nameProject = (TextView)findViewById(R.id.valueNameProject);
		id=(TextView)findViewById(R.id.valueIDProject);
		projectName=(TextView)findViewById(R.id.valueIDProjectName);
		manager =(TextView)findViewById(R.id.valueManager);
		status =(TextView)findViewById(R.id.valueStatus);
		statusDate =(TextView)findViewById(R.id.valueStatusDate );
		
		Intent intent = this.getIntent();
		prProject = (PrProject)intent.getSerializableExtra("project");
		emp = (Employee)intent.getSerializableExtra("emp");
		nameProject.setText(prProject.getNameProject());
		
		id.setText(prProject.getIdProject());
		projectName.setText(prProject.getNameProject());
		manager.setText(prProject.getManager());
		if(prProject.getStatus()==0){
			status.setText(language.getString("notcomplete"));
		}else{
			status.setText(language.getString("complete"));
		}
		statusDate.setText(prProject.getDateStart() + "");
		
		
		bttMember =(RadioButton)findViewById(R.id.radioMember);
		bttTester =(RadioButton)findViewById(R.id.radioTester);
		bttError = (RadioButton)findViewById(R.id.radioError);
		bttMember.setOnClickListener(listenerMember);
		bttTester.setOnClickListener(listernerTester);
		bttError.setOnClickListener(listenerError);
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());

//		report=(TextView)findViewById(R.id.report);report.setText("22");
	}
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(ProjectManagerDetail.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	
	private OnClickListener listenerMember = new OnClickListener() {
		
		public void onClick(View v) {
			if(bttMember.isChecked()){
				Intent intent = new Intent(ProjectManagerDetail.this,ListMemberProject.class);				
				intent.putExtras(getIntent().getExtras());
				intent.putExtra("prproject", prProject);
				intent.putExtra("database", getDatabase());
				intent.putExtra("emp", emp);
				intent.putExtra("lang", lang);
				
				startActivity(intent);
				bttTester.setChecked(false);
				bttError.setChecked(false);
			}
			
		}
	};
	private OnClickListener listernerTester = new OnClickListener() {
		
		public void onClick(View v) {
			if(bttTester.isChecked()){
				bttMember.setChecked(false);
				bttError.setChecked(false);
				Intent intent = new Intent(ProjectManagerDetail.this,ListTesterProject.class);
				intent.putExtras(getIntent().getExtras());
				intent.putExtra("prproject", prProject);
				intent.putExtra("database", getDatabase());
				intent.putExtra("emp", emp);
				intent.putExtra("lang", lang);
				
				startActivity(intent);
			}
		}
	};
	
	private OnClickListener listenerError = new OnClickListener() {
		
		public void onClick(View v) {
			if(bttError.isChecked()){
				bttMember.setChecked(false);
				bttTester.setChecked(false);
				Intent intent = new Intent(ProjectManagerDetail.this,ListErrorProjectManager.class);	
				intent.putExtras(getIntent().getExtras());
				intent.putExtra("prproject", prProject);
				intent.putExtra("empl", emp);
				intent.putExtra("database", getDatabase());
				intent.putExtra("lang", lang);
				
				startActivity(intent);
			}
		}
	};

	public Bundle getAlllangs() {
		if (isRead()) {
			LangDAO lang = new LangDAO();
			Bundle resultVN = new Bundle();
			Bundle resultEN = new Bundle();
			Bundle resultKR = new Bundle();
			try {
				ArrayList<Languages> arr = lang.allLang();
				for (int i = 0; i < arr.size(); i++) {
						resultVN.putString(arr.get(i).getId(), arr.get(i).getVn());
						resultEN.putString(arr.get(i).getId(), arr.get(i).getEn());
						resultKR.putString(arr.get(i).getId(), arr.get(i).getKr());
					};

			} catch (Exception E) {
				E.printStackTrace();
			}
			alllangs.putBundle("VN", resultVN);
			alllangs.putBundle("EN", resultEN);
			alllangs.putBundle("KR", resultKR);
		}
		setRead(false);
		return alllangs;
	}
	public boolean isRead() {
		return read;
	}
	public void setRead(boolean read) {
		this.read = read;
	}
	
	public void initAttribute(){
//		Bundle bundle = this.getAlllangs().getBundle(lang);
		TextView prNamr = (TextView)findViewById(R.id.textProjectName);
		prNamr.setText(language.getString("ac_SUProjectName"));
		
		TextView mag = (TextView)findViewById(R.id.textManager);
		mag.setText(language.getString("ac_Manager"));
		
		TextView status = (TextView)findViewById(R.id.textStatus);
		status.setText(language.getString("ac_Status"));
		
		TextView statusDate = (TextView)findViewById(R.id.textStatusDate);
		statusDate.setText(language.getString("start_date"));
		
		RadioButton rdMember = (RadioButton)findViewById(R.id.radioMember);
		rdMember.setText(language.getString("member"));
		
		RadioButton rdTester = (RadioButton)findViewById(R.id.radioTester);
		rdTester.setText(language.getString("tester"));
		
		RadioButton rdErr = (RadioButton)findViewById(R.id.radioError);
		rdErr.setText(language.getString("error"));
	}
}
